﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace gamexpert
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        public TopMenuBar tmb;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            //put banner

                DataSet1 ds = new DataSet1();
                DataSet1TableAdapters.my_productTableAdapter da = new DataSet1TableAdapters.my_productTableAdapter();
                try
                {
                    da.Fill(ds.my_product);
                }
                catch (Exception ex)
                {
                }
                Random ran = new Random();
                int myi = ran.Next(0, ds.my_product.Rows.Count - 4);
                //imgarr = new List<ImageButton>();
                for (int i = 0; i < 3; i++)
                {
                    /*
                    ImageButton ib = new ImageButton();
                    ib.ID = "i_" + ds.my_product.Rows[myi]["id"].ToString();
                    ib.ImageUrl = "~/images/" + ds.my_product.Rows[myi]["image"];
                    ib.CommandName = "gotopurchase";
                    ib.CommandArgument = ds.my_product.Rows[myi]["id"].ToString();
                    ib.Click += new ImageClickEventHandler(ib_click);
                    ib.Width = Unit.Pixel(126);
                     * */
                    Image ib = new Image();
                    ib.ID = "i_" + ds.my_product.Rows[myi]["id"].ToString();
                    ib.ImageUrl = "~/images/" + ds.my_product.Rows[myi]["image"];
                    ib.Width = Unit.Pixel(126);
                    ib.AlternateText = ds.my_product.Rows[myi]["title"].ToString();
                    HyperLink hl = new HyperLink();
                    
                    hl.Controls.Add(ib);
                    hl.NavigateUrl = "~/Purchase.aspx?cat=" + ds.my_product.Rows[myi]["category"] + "&id=" + ds.my_product.Rows[myi]["id"].ToString();
                    Panel1.Controls.Add(hl);
                    myi++;
                }

            tmb = (TopMenuBar)Session["TopMenuBar"];
            tmb.TopMenuBarChangeEvent += new TopMenuBar.TopMenuBarChangeEventHandler(TopMenuBarChanged);
            tmb.fireTopMenuBarChangeEvent();

            //select active menu
            if (Request["cat"] != null)
            {
                if (Request["cat"] == "Games")
                {
                    tmb.MenuStatus = menuType.Games;
                }
                else if (Request["cat"] == "Accessories")
                {
                    tmb.MenuStatus = menuType.Accessories;
                }
            }
            
            
           

           Cart c = ((Cart)Session["cart"]);
           c.changeEvent += new Cart.cartChangeEventHandler(onCartItemChanged);
           c.fireChangeEvent();//manually fire change event

            //fill search box
           if (Request["search"] != null && !IsPostBack)
           {
               txtSearch.Text = Request["search"];
           }
        }
        //List<ImageButton> imgarr;
        public void ib_click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("Purchase.aspx?id=" + ((ImageButton)sender).CommandArgument);
        }
        protected void onCartItemChanged(object sender, cartChangeEventArgs e)
        {
            Cart c =(Cart)sender;
            GeneralHelper.renderItemsInCart(c,Literal1);
        }
        protected void TopMenuBarChanged(object sender, TopMenuBarChangeEventArgs e)
        {
            string myclass = "selected_menu";
            switch (tmb.MenuStatus)
            {
                case menuType.Aboutus:
                    lnkAboutus.CssClass = myclass;
                    break;
                case menuType.Games:
                    lnkGames.CssClass = myclass;
                    break;
                case menuType.Accessories:
                    lnkAccessories.CssClass = myclass;
                    break;
                case menuType.Contactus:
                    lnkContactus.CssClass = myclass;
                    break;
                case menuType.Home:
                    lnkHome.CssClass = myclass;
                    break;
            }
        }
        protected void lnkAboutus_Click(object sender, EventArgs e)
        {
            tmb.MenuStatus = menuType.Aboutus;
            Response.Redirect("~/AboutUs.aspx");
        }
        protected void lnkHome_Click(object sender, EventArgs e)
        {
            tmb.MenuStatus = menuType.Home;
            Response.Redirect("~/Default.aspx");
        }
        protected void lnkGames_Click(object sender, EventArgs e)
        {
            tmb.MenuStatus = menuType.Games;
            Response.Redirect("~/Purchase.aspx?cat=Games");
        }
        protected void lnkAccessories_Click(object sender, EventArgs e)
        {
            tmb.MenuStatus = menuType.Accessories;
            Response.Redirect("~/Purchase.aspx?cat=Accessories");
        }
        protected void lnkContactus_Click(object sender, EventArgs e)
        {
            tmb.MenuStatus = menuType.Contactus;
            Response.Redirect("~/ContactUs.aspx");
        }
        protected void LoginStatus1_PreRender(object sender, EventArgs e)
        {
            tmb.MenuStatus = menuType.None;
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            tmb.MenuStatus = menuType.None;
            Response.Redirect("~/Purchase.aspx?search=" + txtSearch.Text);
        }
        protected void lbtn_account_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/member/MemberArea.aspx");
        }
    }
}